<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 公告接口
 * @ApiInternal
 */
class Notice extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Notice;
    }

    public function category()
    {
        $list = \app\common\model\Category::all(['type' => 'notice']);
        foreach ($list as $row) {
            $row->visible(['id', 'name']);
        }

        $this->success('success', $list);
    }

    public function index()
    {
        $category_id = $this->request->get('category_id', '');
        $type = $this->request->get('type', '');

        $where = ['status' => 1];
        $category_id != '' && $where['category_id'] = $category_id;
        $type != '' && $where['type'] = Db::raw("FIND_IN_SET('" . $type . "', type)");

        $list = $this->model->where($where)->order('createtime', 'desc')->paginate();
        foreach ($list as $row) {
            $row->visible(['id', 'title', 'desc', 'createtime']);
        }

        $this->success('success', $list);
    }

    public function detail()
    {
        $id = $this->request->get('id', '');
        $row = $this->model->get(['id' => $id, 'status' => 1]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $this->success('success', $row);
    }
}